/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.money.controller;

import com.money.biz.TypeBiz;
import com.money.entity.Type;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Eason
 */
@Controller
public class TypeController {

    @Resource(name = "typeBiz")
    private TypeBiz typeBiz;

    @RequestMapping(value = "/addType.do", method = RequestMethod.POST)
    public ModelAndView addType(Type type, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView mav = new ModelAndView();
        //增加的typeID唯一
        if (typeBiz.getSubTypeByType(type.getTypeID()).isEmpty()) {
            typeBiz.addType(type);
            String url = "redirect:/typeManage.do";
            mav = new ModelAndView(url);
            return mav;
        } else {
            response.reset();
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write("<script>alert('此类型ID已存在！');</script>");
            out.write("<script>history.go(-1);</script>");
            out.close();
            return null;
        }
    }

    @RequestMapping(value = "/getTypeforEdit.do", method = RequestMethod.GET)
    public ModelAndView getTypeForEdit(String typeID, HttpServletRequest request, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        ArrayList<Type> typeList = (ArrayList<Type>) typeBiz.getTypeByTypeID(typeID);
        session.setAttribute("type", typeList.get(0));
        mav.setViewName("/admin/tab/editType");
        return mav;
    }

    @RequestMapping(value = "/editType.do", method = RequestMethod.POST)
    public ModelAndView editType(Type type, HttpServletRequest request, HttpSession session) {

        ModelAndView mav = new ModelAndView();
        ArrayList<Type> typeList = (ArrayList<Type>) typeBiz.getTypeByTypeID(type.getTypeID());

        for (int i = 0; i < typeList.size(); i++) {
            typeList.get(i).setTypeID(type.getTypeID());
            typeList.get(i).setTypeName(type.getTypeName());
            typeBiz.editType(typeList.get(i));
        }

        String url = "redirect:/typeManage.do";
        mav = new ModelAndView(url);
        return mav;
    }

    @RequestMapping(value = "/removeType.do", method = RequestMethod.GET)
    public ModelAndView removeType(int id) {
        ModelAndView mav = new ModelAndView();
        Type type = typeBiz.getTypeByID(id);
        ArrayList<Type> typeList = (ArrayList<Type>) typeBiz.getSubTypeByType(type.getTypeID());
        if (typeList.size() > 1) {
            for (int i = 0; i < typeList.size(); i++) {
                typeList.get(i).setTypeID("Deleted");
                typeList.get(i).setTypeName("已删除");
                typeBiz.editType(typeList.get(i));
            }
        }
        typeBiz.removeType(id);

        String url = "redirect:/typeManage.do";
        mav = new ModelAndView(url);
        return mav;
    }

    @RequestMapping(value = "/typeManage.do")
    public ModelAndView manage(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        ArrayList<Type> allTypeList = (ArrayList<Type>) typeBiz.getAllType();
        session.setAttribute("typeList", allTypeList);
        mav.setViewName("admin/tab/type");
        return mav;
    }
/**
 * 以下是子类别的方法
 * @param request
 * @param response
 * @param session
 * @return 
 */
    @RequestMapping(value = "/manageSubtype.do")
    public ModelAndView manageSubtype(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        String typeID = null;
        //从添加、编辑子类别跳回页面时避免丢失typeID
        if (session.getAttribute("typeIdForDisplay") != null) {
            typeID = (String) session.getAttribute("typeIdForDisplay");
            session.removeAttribute("typeIdForDisplay");
        } else {
            typeID = request.getParameter("selectType");
        }
        System.out.println(typeID+"``````````````");
        ArrayList<Type> subtypeList = (ArrayList<Type>) typeBiz.getTypeByTypeID(typeID);
        session.setAttribute("subtypeList", subtypeList);
        mav.setViewName("admin/tab/subType");
        return mav;
    }

    @RequestMapping(value = "/addSubtype.do", method = RequestMethod.POST)
    public ModelAndView addSubtype(Type type, HttpSession session) {
        ArrayList<Type> typeList = (ArrayList<Type>) session.getAttribute("subtypeList");
        type.setTypeID(typeList.get(0).getTypeID());
        type.setTypeName(typeList.get(0).getTypeName());
        typeBiz.addType(type);
        session.setAttribute("typeIdForDisplay", type.getTypeID());
        String url = "redirect:/manageSubtype.do";
        ModelAndView mav = new ModelAndView(url);
        return mav;
    }

    @RequestMapping(value = "/getsubTypeforEdit.do", method = RequestMethod.GET)
    public ModelAndView getsubTypeforEdit(int id, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        Type type = typeBiz.getTypeByID(id);
        session.setAttribute("subtype", type);
        mav.setViewName("/admin/tab/editSubtype");
        return mav;
    }

    @RequestMapping(value = "/editSubtype.do", method = RequestMethod.POST)
    public ModelAndView editSubtype(Type type, HttpServletRequest request, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        Type firstType = (Type) session.getAttribute("subtype");
        Type getTypeName = (Type) typeBiz.getTypeByTypeID(type.getTypeID()).get(0);
        type.setTypeName(getTypeName.getTypeName());
        type.setId(firstType.getId());
        typeBiz.editType(type);
        session.setAttribute("typeIdForDisplay", type.getTypeID());
        String url = "redirect:/manageSubtype.do";
        mav = new ModelAndView(url);
        return mav;
    }
    
        @RequestMapping(value = "/removeSubtype.do", method = RequestMethod.GET)
    public ModelAndView removeSubtype(int id, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        Type type = typeBiz.getTypeByID(id);
        session.setAttribute("typeIdForDisplay", type.getTypeID());
        String url = "redirect:/manageSubtype.do";
        typeBiz.removeType(id);
        mav = new ModelAndView(url);
        return mav;
    }
}
